//
//  MetaVersionController.m
//  iGSS
//
//  Copyright 2010 Electronic Business Systems Ltd.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import "MetaVersionController.h"
#import "FormattingUtilities.h"
#import "GSSCommunicator.h"
#import "GSSObject.h"

@implementation MetaVersionController

- (void)dealloc {
    [versioningSwitch release];
    [super dealloc];
}

- (NSInteger)numberOfRows {
    if (object.versioned)
        return 2;
    else
        return 1;
}

- (UITableViewCell *)cellForRow:(NSInteger)row {
    static NSString *CellIdentifier = @"MetaVersionCell";
    
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
                                       reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:13.0];
    }
        
    switch (row) {
        case 0:
            if (!versioningSwitch) {
                versioningSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
                [versioningSwitch addTarget:self
                                     action:@selector(versioningChanged)
                           forControlEvents:UIControlEventValueChanged];                
            }            
            versioningSwitch.on = object.versioned;
            versioningSwitch.enabled = self.isEditing;

            cell.textLabel.text = LocalizedString(@"Versioning");
            cell.detailTextLabel.text = nil;
            cell.accessoryView = versioningSwitch;
            cell.editingAccessoryView = versioningSwitch;
            break;
        
        case 1:
            cell.textLabel.text = LocalizedString(@"Current");
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", object.version];

            break;
    }
    
    return cell;
}


//MARK: -

- (void)versioningChanged {
    if (versioningSwitch.on) {
        [[GSSCommunicator defaultCommunicator] setVersioned:YES ofObject:object];
        object.versioned = YES;
        [object save];
        [self.tableView reloadData];
    } else {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:LocalizedString(@"Delete all versions")
                                        message:LocalizedString(@"{Turn versioning off?}")
                                       delegate:self
                              cancelButtonTitle:LocalizedString(@"Cancel")
                              otherButtonTitles:LocalizedString(@"OK"), nil];
        [alertView show];
    }
}


//MARK: -
//MARK: UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        [versioningSwitch setOn:YES animated:YES];
    } else {
        [[GSSCommunicator defaultCommunicator] setVersioned:NO ofObject:object];
        object.versioned = NO;
        object.version = 1;
        [object save];
        [self.tableView reloadData];        
    }
}

@end
